package com.zhoug.widget;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;


import com.zhoug.common.utils.AppUtils;

import androidx.appcompat.widget.LinearLayoutCompat;

/**
 * @Author HK-LJJ
 * @Date 2020/1/15
 * @Description
 */
public class SearchView extends LinearLayoutCompat implements TextView.OnEditorActionListener, View.OnClickListener {
    private EditText mEtInput;
    private ImageView mIvSearch;
    private ImageView mIvClear;
    private ViewGroup mRoot;
    private OnSearchListener onSearchListener;
    private String queryHintText;//提示
    private int queryTextColor;//输入颜色
    private int queryHintTextColor;//提示颜色
    private float queryTextSize;

    public SearchView(Context context) {
        this(context, null);
    }

    public SearchView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);

    }

    public SearchView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        View view = LayoutInflater.from(getContext()).inflate(R.layout.widgets_search_view, null);
        addView(view);
        mEtInput = findViewById(R.id.et_input);
        mIvSearch = findViewById(R.id.iv_search);
        mIvClear = findViewById(R.id.iv_clear);
        mRoot = findViewById(R.id.root);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.SearchView);
        Drawable frameDrawable = typedArray.getDrawable(R.styleable.SearchView_frameDrawable);
        if (frameDrawable != null) {
            mRoot.setBackground(frameDrawable);
        }
        int queryTextColor = typedArray.getColor(R.styleable.SearchView_queryTextColor, 0);
        int queryHintTextColor = typedArray.getColor(R.styleable.SearchView_queryHintTextColor, 0);
        float queryTextSize = typedArray.getDimension(R.styleable.SearchView_queryTextSize, 0.0f);
        String queryHintText = typedArray.getString(R.styleable.SearchView_queryHintText);
        int queryIconSize = typedArray.getDimensionPixelOffset(R.styleable.SearchView_queryIconSize, 0);
        Drawable queryIcon = typedArray.getDrawable(R.styleable.SearchView_queryIcon);

        if (queryTextColor != 0) {
            setQueryTextColor(queryTextColor);
        }
        if (queryHintTextColor != 0) {
            setQueryHintTextColor(queryHintTextColor);
        }
        if (queryTextSize != 0) {
            setQueryTextSize(queryTextSize);
        }
        if (queryHintText != null) {
            setQueryHintText(queryHintText);
        }

        if (queryIconSize != 0) {
            ViewGroup.LayoutParams layoutParams = mIvSearch.getLayoutParams();
            layoutParams.height = queryIconSize;
            layoutParams.width = queryIconSize;
            mIvSearch.setLayoutParams(layoutParams);
        }
        if (queryIcon != null) {
            mIvSearch.setImageDrawable(queryIcon);
        }

        typedArray.recycle();
        init();
    }


    private void init() {
        mEtInput.setOnEditorActionListener(this);
        mEtInput.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s != null && !s.toString().isEmpty()) {
                    if (mIvClear.getVisibility() != VISIBLE) {
                        mIvClear.setVisibility(VISIBLE);
                    }
                } else if (mIvClear.getVisibility() == VISIBLE) {
                    mIvClear.setVisibility(GONE);
                }
            }
        });
//        mIvSearch.setOnClickListener(this);
        mIvClear.setOnClickListener(v -> {
            mEtInput.setText("");

        });
    }


    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.iv_search) {
            search();
        }
    }

    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        int searchId = getResources().getInteger(R.integer.widgets_ime_search_id);
        if (actionId == searchId || actionId == EditorInfo.IME_ACTION_SEARCH) {
            search();
            return true;
        }
        return false;
    }

    private void search() {
        mEtInput.clearFocus();
        if (getContext() instanceof Activity) {
            AppUtils.hideSoftKeyboard((Activity) getContext());
        }
        if (onSearchListener != null) {
            onSearchListener.onSearch(getInput());
        }
    }

    /**
     * 获取输入值
     *
     * @return
     */
    public String getInput() {
        if (mEtInput == null) {
            return null;
        }
        return mEtInput.getText().toString();
    }

    /**
     * 设置查询提示
     *
     * @param queryHintText
     */
    public void setQueryHintText(String queryHintText) {
        this.queryHintText = queryHintText;
        if (mEtInput != null) {
            mEtInput.setHint(this.queryHintText);
        }
    }

    /**
     * 设置输入颜色
     *
     * @param queryTextColor
     */
    public void setQueryTextColor(int queryTextColor) {
        this.queryTextColor = queryTextColor;
        if (mEtInput != null) {
            mEtInput.setTextColor(queryTextColor);
        }
    }

    /**
     * 设置提示颜色
     *
     * @param queryHintTextColor
     */
    public void setQueryHintTextColor(int queryHintTextColor) {
        this.queryHintTextColor = queryHintTextColor;
        if (mEtInput != null) {
            mEtInput.setHintTextColor(queryHintTextColor);
        }
    }

    /**
     * 设置输入文本大小
     *
     * @param queryTextSize px
     */
    public void setQueryTextSize(float queryTextSize) {
        this.queryTextSize = queryTextSize;
        if (mEtInput != null) {
            mEtInput.setTextSize(TypedValue.COMPLEX_UNIT_PX, queryTextSize);

        }
    }


    public interface OnSearchListener {
        void onSearch(String input);
    }


    public void setOnSearchListener(OnSearchListener onSearchListener) {
        this.onSearchListener = onSearchListener;
    }

}
